package view;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.ArrayList;

import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JCheckBox;

import javax.swing.JFrame;

import org.bushe.swing.event.EventBus;

import constants.Events;
import constants.Rules;


/**
 * This code was edited or generated using CloudGarden's Jigloo
 * SWT/Swing GUI Builder, which is free for non-commercial
 * use. If Jigloo is being used commercially (ie, by a corporation,
 * company or business for any purpose whatever) then you
 * should purchase a license for each developer using Jigloo.
 * Please visit www.cloudgarden.com for details.
 * Use of Jigloo implies acceptance of these licensing terms.
 * A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
 * THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
 * LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
 */
@SuppressWarnings("serial")
public class RuleOptionsView extends JFrame implements KeyListener {

    private AbstractAction okButtonAction;
    private AbstractAction cancelAction;
    private AbstractAction enterGoalEasilyAction;
    private AbstractAction allowPushAction;
    private JButton okButton;
    private JButton cancelButton;
    private JCheckBox jCheckAllowPush;
    private JCheckBox jCheckEnterEasy;

    private ArrayList<Rules> r = new ArrayList<Rules>();

    public RuleOptionsView() {
        initGUI();
        setVisible(true);
        setLocationRelativeTo(null);
        setFocusable(true);
        addKeyListener(this);

        jCheckAllowPush.setSelected(true);
    }

    private void initGUI() {
        try {
            {
                getContentPane().setLayout(null);
                this.setPreferredSize(new java.awt.Dimension(400, 400));
                this.setTitle("Set Game Rules");
                this.setResizable(false);
                {
                    okButton = new JButton();
                    getContentPane().add(okButton);
                    getContentPane().add(getAllowPush());
                    getContentPane().add(getAllowEnterGoalEasy());
                    getContentPane().add(getCancelButton());
                    okButton.setBounds(313, 318, 55, 35);
                    okButton.setAutoscrolls(true);
                    okButton.setAction(getOkButtonAction());
                    okButton.setText("OK");
                }
                this.setSize(400, 400);
            }
        } catch(Exception e) {
            e.printStackTrace();
        }
    }

    private AbstractAction getOkButtonAction() {
        if(okButtonAction == null) {
            okButtonAction = new AbstractAction("OK", null) {
                public void actionPerformed(ActionEvent evt) {

                    EventBus.publish(Events.CHANGE_RULES.toString(), r);
                    //TODO have this do something (issue44)
                    setVisible(false);
                    r.clear();
                }
            };
        }
        return okButtonAction;
    }

    private JCheckBox getAllowPush() {
        if(jCheckAllowPush == null) {
            jCheckAllowPush = new JCheckBox();
            jCheckAllowPush.setText("Allow Push");
            jCheckAllowPush.setBounds(12, 25, 152, 20);
            jCheckAllowPush.setToolTipText("Allows you to push a foe's player piece back to his/her home.");
            jCheckAllowPush.setAction(getAllowPushAction());
            jCheckAllowPush.setMnemonic('a');
        }
        return jCheckAllowPush;
    }

    private JCheckBox getAllowEnterGoalEasy() {
        if(jCheckEnterEasy == null) {
            jCheckEnterEasy = new JCheckBox();
            jCheckEnterEasy.setText("Enter Goal Easily");
            jCheckEnterEasy.setBounds(12, 64, 152, 20);
            jCheckEnterEasy.setToolTipText("With this setting checked, you can enter the goal eventhough you might get a die value taking you to far.");
            jCheckEnterEasy.setAction(getEnterGoalEasilyAction());
            jCheckEnterEasy.setMnemonic('e');
        }
        return jCheckEnterEasy;
    }

    private JButton getCancelButton() {
        if(cancelButton == null) {
            cancelButton = new JButton();
            cancelButton.setText("Cancel");
            cancelButton.setBounds(224, 318, 78, 35);
            cancelButton.setAction(getCancelAction());
        }
        return cancelButton;
    }

    private AbstractAction getAllowPushAction() {
        if(allowPushAction == null) {
            allowPushAction = new AbstractAction("Allow Push", null) {
                public void actionPerformed(ActionEvent evt) {

                    if(r.contains(constants.Rules.PUSH))
                        r.remove(constants.Rules.PUSH);
                    else
                        r.add(constants.Rules.PUSH);
                }
            };
        }
        return allowPushAction;
    }

    private AbstractAction getEnterGoalEasilyAction() {
        if(enterGoalEasilyAction == null) {
            enterGoalEasilyAction = new AbstractAction("Enter Goal Easily", null) {
                public void actionPerformed(ActionEvent evt) {
                    if(r.contains(constants.Rules.ENTEREASY))
                        r.remove(constants.Rules.ENTEREASY);
                    else
                        r.add(constants.Rules.ENTEREASY);
                }
            };
        }
        return enterGoalEasilyAction;
    }

    private AbstractAction getCancelAction() {
        if(cancelAction == null) {
            cancelAction = new AbstractAction("Cancel", null) {
                public void actionPerformed(ActionEvent evt) {
                    setVisible(false);
                }
            };
        }
        return cancelAction;
    }

    @Override
    public void keyPressed(KeyEvent e) {
        Character c = e.getKeyChar();
        if(c.equals('\n')) {
            okButton.doClick();
        }
        System.out.println(e.getKeyCode());
        if(e.getKeyCode() == 27)
            cancelButton.doClick();

    }

    @Override
    public void keyReleased(KeyEvent e) {}

    @Override
    public void keyTyped(KeyEvent e) {}
}
